package com.itfreer.gis;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * arcgis 访问代理
 */
@Component
@Path("/proxy/")
public class ArcgisProxy {

	@Autowired(required = false)
	private ProxyPara proxyPara;

	@POST
	@Path("/proxy_arcgis")
	public void proxyPost(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		proxy(request, response);
	}
	
//	@GET
//	@Path("/proxy_arcgis")
//	public void proxyGet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//		proxy(request, response);
//	}
	
	private void proxy(HttpServletRequest request, HttpServletResponse response) {
		String[] serverUrls = {};
		if (proxyPara != null) {
			serverUrls = new String[proxyPara.getProxyUrls().size()];
			proxyPara.getProxyUrls().toArray(serverUrls);
		}

		try {
			String reqUrl = request.getQueryString();
			System.out.println(reqUrl);
			boolean allowed = false;
			String token = null;
			for (String surl : serverUrls) {
				String[] stokens = surl.split("\\s*,\\s*");
				System.out.println(stokens[0]);
				if (reqUrl.toLowerCase().contains(stokens[0].toLowerCase())) {
					allowed = true;
					if (stokens.length >= 2 && stokens[1].length() > 0) {
						token = stokens[1];
					}
					break;
				}
			}
			if (reqUrl.endsWith(".png")) {
				allowed = true;
			}

			if (!allowed) {
				response.setStatus(403);
				return;
			}
			if (token != null) {
				reqUrl = reqUrl + (reqUrl.indexOf("?") > -1 ? "&" : "?") + "token=" + token;
			}
			URL url = new URL(reqUrl);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setDoOutput(true);
			con.setRequestMethod(request.getMethod());
			if (request.getContentType() != null) {
				con.setRequestProperty("Content-Type", request.getContentType());
			}
			con.setRequestProperty("Referer", request.getHeader("Referer"));
			int clength = request.getContentLength();
			if (clength > 0) {
				con.setDoInput(true);
				InputStream istream = request.getInputStream();
				OutputStream os = con.getOutputStream();
				final int length = 5000;
				byte[] bytes = new byte[length];
				int bytesRead = 0;
				while ((bytesRead = istream.read(bytes, 0, length)) > 0) {
					os.write(bytes, 0, bytesRead);
				}
			} else {
				con.setRequestMethod("GET");
			}
			// out.clear();
			// out = pageContext.pushBody();
			OutputStream ostream = response.getOutputStream();
			response.setContentType(con.getContentType());
			InputStream in = con.getInputStream();
			final int length = 5000;
			byte[] bytes = new byte[length];
			int bytesRead = 0;
			while ((bytesRead = in.read(bytes, 0, length)) > 0) {
				ostream.write(bytes, 0, bytesRead);
				try {
					String temp = new String(bytes);
					System.out.println(temp);
				} catch (Exception e) {

				}
			}
		} catch (Exception e) {
			e.getStackTrace();
			response.setStatus(500);
		}
	}
}